const { log } = require("console")
const fs=require("fs")
const path=require("path")
const { join } = require("path/posix")

const regstyle=/<style>[\s\S]*<\/style>/
const regScript=/<script>[\s\S]*<\/script>/

fs.readFile(path.join(__dirname,"../素材/index.html"),"utf8",function (err,dataStr) {
if (err) return console.log("读取HTML文件失败!" + err.message)

    resolveCSS(dataStr)
    resolveJS(dataStr)
    resolveHTML(dataStr)
})


function resolveCSS(htmlStr) {
   const r1 = regstyle.exec(htmlStr)
//    console.log(r1);
const newCSS=r1[0].replace("<style>",'').replace("</style>","")


//    console.log(newCSS);
fs.writeFile(path.join(__dirname,"clock/index.css"),newCSS,function(err){
    if(err) return console.log("CSS写入错误"+err.message)
        console.log("CSS写入成功");      
})
}
function resolveJS(htmlJS){
   const r2= regScript.exec(htmlJS)
    const newJS=r2[0].replace("<script>","").replace("</script>","")
    fs.writeFile(path.join(__dirname,"clock/index.js"),newJS,function (err) {
        if (err) return console.log("HTML写入错误",err.message)
            console.log("JS写入成功")
    })
}
function resolveHTML(dataStr){
    const newHTML=dataStr.replace(regstyle,"<link rel='stylesheet' href='./index.css' />").replace(regScript,"<script src='./index.js'></script>")
    fs.writeFile(path.join(__dirname,'clock/index.html'),newHTML,function (err) {
        if (err) return console.log('写入HTML 文件失败！' + err.message)
            console.log('写入HTML成功！');
            
        
    })

}